#include"myshell.h"
#include<stdlib.h>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iostream>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
//命令行参数，弄成全局的
char* gargv[ARGS] = {NULL};
int gargc = 0;//命令行参数的个数
//设置一个全局的pwd环境变量
char pwd[2048];

//这三个函数我只想让内部访问
static std::string GetUserName(){
    char* user_ptr = getenv("USER");
    std::string username = user_ptr ? user_ptr : "default_user";
    return username;
}
static std::string GetHostName(){
    char*  hostname_ptr = getenv("HOSTNAME");
    std::string hostname = hostname_ptr ? hostname_ptr:"None";
    return hostname;
}
//工作路径
static std::string GetPwd(){
    //char* pwd_ptr = getenv("PWD");
    //std::string pwd = pwd_ptr ? pwd_ptr : "None";
    char buff[1024];
    if(getcwd(buff,sizeof(buff))){
        //顺便更新一下环境变量
        snprintf(pwd,sizeof(pwd),"PWD=%s",buff);
        putenv(pwd);
        return std::string(buff);
    }
    else{
       return "None";
    }
}
static std::string GetHomePath(){
    char* Home_ptr = getenv("HOME");
    std::string home = Home_ptr ? Home_ptr : "Node";
    return home;
}
//命令行打印
void CommandLinePrompt(){
    std::string user = GetUserName();
    std::string hostname = GetHostName();
    std::string pwd = GetPwd();
    printf("[%s@%s %s]# ",user.c_str(),hostname.c_str(),pwd.c_str());
}
//获取命令行的输入
bool GetCommandString(char cmd_str_buffer[],int len){
    if(cmd_str_buffer == NULL || len<= 0){
        return false;
    }
    //fets将命令行获取到cmd_str_buffer这个数组中
    char* sec = fgets(cmd_str_buffer,len,stdin);
    if(sec == NULL){
        return false;
    }
    //将最后的一个回车换成\0；
    cmd_str_buffer[strlen(cmd_str_buffer)-1] = 0;

    //统计有效的字符的个数strlen
    return strlen(cmd_str_buffer) == 0? false:true; 
}
//分割字符串
bool PraseCommandString(char* cmd){
       //检查
    if(cmd == NULL){
       return false;
   }
   else{
#define SEC " "
       gargv[gargc++] = strtok(cmd,SEC);
       //将剩下所有的分割
       while((bool)(gargv[gargc++] = strtok(NULL,SEC)));
       //倒退一次
       gargc--;
//#define DEBUG 调试代码
#ifdef DEBUG
       printf("gargc = %d\n",gargc);
       for(int i = 0;i<gargc;i++){
           printf("gargv[%d]:%s\n",i,gargv[i]);
       }
#endif
       return true;
   }
}
//初始化全局变量
void InitGlobal(){
    gargc = 0;
    memset(gargv,0,sizeof(gargc));
}
//执行指令
void ForkAndExe(){
    //创建子进程
    pid_t id  = fork();
    if(id < 0){
        perror("fork");
        return ;
    }
    else if(id == 0)
    {
        //子进程执行
        execvp(gargv[0],gargv);
        exit(0);
    }
    else{
        //父进程等待
        pid_t rid = waitpid(id,NULL,0);
        if(rid > 0){
            //等待成功
            
        }
    }
} 
    //内建命令的判断和执行
bool BulidInCommandExec(){
    std::string cmd = gargv[0];
    bool ret = false;
    if(cmd == "cd"){
        if(gargc == 2){
            ret = true;
            chdir(gargv[1]);
        }
        else if(gargc == 1){
            ret = true;
            chdir(GetHomePath().c_str());
        }
        else {
            //输入有问题
            printf("不存在该指令!\n");
        }
    }
        return ret;
    
}










